<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div>
    <p>用户名：<input id="username" type="text" /></p>
    <p>密码：<input id="password" type="password" /></p>
    <p><input id="submitBtn" type="button" value="提交"></p>
  </div> 
<script>
let username = document.getElementById('username');
let password = document.getElementById('password');
let submitBtn = document.getElementById('submitBtn');
Function.prototype.before = function(beforefn) {
  let __self = this;
  return function() {
    if(beforefn.apply(this, arguments) === false) {
      return 
    }
    return __self.apply(this, arguments);
  }
};
let validate = function() {
  if(username.value === '') {
    alert('用户名不能为空')
    return false
  }
  if(password.value === '') {
    alert('密码不能为空')
    return false
  }
}
let formSubmit = function() {
  let param = {
    username: username.value,
    password: password.value
  }
  console.log(param);
}
formSubmit = formSubmit.before(validate)
submitBtn.onclick = function() {
  formSubmit()
}
</script>
</body>
</html>